package bingo.web.mvc.plugin;

import java.sql.Clob;
import java.sql.SQLException;
import java.util.Date;

import javax.servlet.http.HttpServlet;
import javax.sql.rowset.serial.SerialClob;
import javax.sql.rowset.serial.SerialException;

import net.bingosoft.common.BeanHelper;
import net.bingosoft.common.ObjectRegistry;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.converters.BigDecimalConverter;
import org.apache.commons.beanutils.converters.BigIntegerConverter;
import org.apache.commons.beanutils.converters.BooleanConverter;
import org.apache.commons.beanutils.converters.ByteConverter;
import org.apache.commons.beanutils.converters.CharacterConverter;
import org.apache.commons.beanutils.converters.DoubleConverter;
import org.apache.commons.beanutils.converters.FloatConverter;
import org.apache.commons.beanutils.converters.IntegerConverter;
import org.apache.commons.beanutils.converters.LongConverter;
import org.apache.commons.beanutils.converters.ShortConverter;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * Jan 21, 2009 11:23:20 AM
 * Author:yangdongyu(yangdy@bingosoft.net)
 * ReadMe:系统初始化的插件
 * ChangeLog:
 */
public class SystemInitPlugin  implements Plugin{
	private static final Log log = LogFactory.getLog(SystemInitPlugin.class);
	
	public void init(HttpServlet servlet){
		log.debug("注册数据转换插件...");
		ConvertUtils.register(dateConverter, Date.class);
		ConvertUtils.register(stringConverter, String.class);
		ConvertUtils.register(clobConvert, Clob.class);
		ConvertUtils.register(new BigDecimalConverter(null), java.math.BigDecimal.class);
		ConvertUtils.register(new BigIntegerConverter(null), java.math.BigInteger.class);
		ConvertUtils.register(new BooleanConverter(null), java.lang.Boolean.class);
		ConvertUtils.register(new ByteConverter(null), java.lang.Byte.class);
		ConvertUtils.register(new CharacterConverter(null), java.lang.Character.class);
		ConvertUtils.register(new DoubleConverter(null), java.lang.Double.class);
		ConvertUtils.register(new FloatConverter(null), java.lang.Float.class);
		ConvertUtils.register(new IntegerConverter(null), java.lang.Integer.class);
		ConvertUtils.register(new LongConverter(null), java.lang.Long.class);
		ConvertUtils.register(new ShortConverter(null), java.lang.Short.class);
		log.debug("注册数据转换插件注册成功...");
		
		ApplicationContext overrideFactory = WebApplicationContextUtils.getWebApplicationContext(servlet.getServletContext());
		if(overrideFactory!=null){
			ObjectRegistry.setOverrideFactory(overrideFactory);
		}
	}
	
	//日期转换类
	Converter dateConverter = new Converter() {
		public Object convert(Class type, Object value) {
			if (value instanceof String && StringUtils.isNotEmpty((String)value)){
				return BeanHelper.parseDate((String) value);
			}else if(value instanceof Date){
				return value;
			}else{
				return null;
			}
		}
	};
	//字符串转换器
	Converter stringConverter = new Converter(){
		public Object convert(Class type, Object value){
			return BeanHelper.converToString(value);
		}
	};
//	大字段转换器
	Converter clobConvert = new Converter(){
		public Object convert(Class type, Object value) {
			if(value == null) return null;
			try {					
				return new SerialClob(BeanHelper.converToString(value).toCharArray()){						
					private static final long serialVersionUID = 1L;
					@Override
					public String toString() {
						String result;
						try {
							return this.getSubString(1, (int)this.length());
						} catch (SerialException e) {
							result = super.toString();
						}
						return result;
					}
				};
			} catch (SQLException e) {
				log.debug("转换大字段(Clob)失败...",e);
			}
			return null;
		}
	};
	
	public void destory(HttpServlet servlet){
		log.debug("重置数据转换插件...");
		ConvertUtils.deregister();
	}
}
